﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Xml.Serialization;

namespace SlaktDataReader
{
    [Serializable]
    public class BirthBook : BookBase<BirthBookRecord>, SlaktDataReader.IBooks
    {
        public override ObservableCollection<BirthBookRecord> Items { get; set; }

        public override BookTypes BookType { get { return BookTypes.Born; } }

        //private object myType = typeof(this.typeof());

        public BirthBook(string FileName)
        {
            this.FileName = FileName;
            _init();
            ReadBook();
        }

        public BirthBook()
        {
            _init();
        }

        public bool Save(string SaveFilename)
        {
            return false;
        }

        protected override void bwLoad_DoWork(object sender, DoWorkEventArgs e)
        {
            BirthBook rv;
            BWArgs arg = e.Argument as BWArgs;

            switch (arg.FileType)
            {
                case BWArgs.BookFileType.Text:
                    rv = _ReadTextFile(arg.FileName);
                    e.Result = rv;
                    break;
                case BWArgs.BookFileType.Excel:
                    rv = _ReadExcelFile(arg.FileName);
                    e.Result = rv;
                    break;
                default:
                    break;
            }


        }

        protected override void bwLoad_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {

            if (e.Result != null)
            {
                BirthBook rv = e.Result as BirthBook;
                if (rv.Items != null)
                {
                    //this.Area = rv.Area;
                    //this.EditedBy = rv.EditedBy;

                    //this.Parish = rv.Parish;
                    //this.Period = rv.SDCode;
                    //this.Source = rv.Source;
                    //this.UpdateDate = rv.UpdateDate;

                    this.Items = rv.Items;

                    readSuccess();

                    return;
                }
            }
            readFail();
        }

        protected override void bwLoad_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
        }

        private BirthBook _ReadTextFile(string fName)
        {
            BirthBookTextFile reader = new BirthBookTextFile();
            BirthBook bBook = new BirthBook();
            if (reader.Read(fName) > 0)
            {
                bBook.Items = new ObservableCollection<BirthBookRecord>(reader.Records);
                bBook.FileName = fName;
                // Copy other data
                return bBook;
            }
            return null;
        }

        private BirthBook _ReadExcelFile(string fName)
        {
            return null;
        }


        public override void ExportBook()
        {
            exportFail();
        }



    }

    [Serializable]
    public class BirthBookRecord
    {
        private int rowID = 0;
        public int RowID { get { return rowID; } set { rowID = value; IsChanged = true; } }

        private string pageNo = null;
        public string PageNo { get { return pageNo; } set { pageNo = value; IsChanged = true; } }

        private string ordNo = null;
        public string OrdNo { get { return ordNo; } set { ordNo = value; IsChanged = true; } }

        private string birthDate = null;
        public string BirthDate { get { return birthDate; } set { birthDate = value; IsChanged = true; } }

        private string christnDate = null;
        public string ChristeningDate { get { return christnDate; } set { christnDate = value; IsChanged = true; } }

        private char gender = 'U';
        public char Gender { get { return gender; } set { gender = value; IsChanged = true; } }

        private string firstName = null;
        public string FirstName { get { return firstName; } set { firstName = value; IsChanged = true; } }

        private string surName = null;
        public string SurName { get { return surName; } set { surName = value; IsChanged = true; } }

        private string address = null;
        public string Address { get { return address; } set { address = value; IsChanged = true; } }

        private string nAddress = null;
        public string NAddress { get { return nAddress; } set { nAddress = value; IsChanged = true; } }

        private string maleTitle = null;
        public string FatherTitle { get { return maleTitle; } set { maleTitle = value; IsChanged = true; } }

        private string mfName = null;
        public string FatherFirstName { get { return mfName; } set { mfName = value; IsChanged = true; } }

        private string msName = null;
        public string FatherSurName { get { return msName; } set { msName = value; IsChanged = true; } }

        private string mAge = "";
        public string FatherAge { get { return mAge; } set { mAge = value; IsChanged = true; } }

        private string fTitle = null;
        public string MotherTitle { get { return fTitle; } set { fTitle = value; IsChanged = true; } }

        private string ffName = null;
        public string MotherFirstName { get { return ffName; } set { ffName = value; IsChanged = true; } }

        private string fsName = "";
        public string MotherSurName { get { return fsName; } set { fsName = value; IsChanged = true; } }

        private string fAge = null;
        public string MotherAge { get { return fAge; } set { fAge = value; IsChanged = true; } }

        private string extra1 = null;
        public string Extra1 { get { return extra1; } set { extra1 = value; IsChanged = true; } }

        private string extra2 = null;
        public string Extra2 { get { return extra2; } set { extra2 = value; IsChanged = true; } }

        private string extra3 = null;
        public string Extra3 { get { return extra3; } set { extra3 = value; IsChanged = true; } }

        private string uppdaterat = "";
        public string Updated { get { return uppdaterat; } set { uppdaterat = value; IsChanged = true; } }

        [XmlIgnore]
        public bool IsChanged { get; set; }
    }
}
